package nc.pubimpl.credit.creditcheck;

import java.util.List;
import nc.vo.credit.creditcheck.entity.CreditCheckMessage;
import nc.vo.credit.pub.ExamItemVO;
import nc.vo.pub.BusinessException;
import nc.vo.pub.lang.UFDouble;
import nc.vo.pubapp.pattern.pub.MathTool;




public class AuditFlowCreditCheck
{
  private boolean isAddSelf;
  private boolean isRateCheck;
  
  public AuditFlowCreditCheck(boolean isRateCheck1, boolean isAddSelf) {
    this.isRateCheck = isRateCheck1;
    this.isAddSelf = isAddSelf;
  }
  
  public UFDouble exam(ExamItemVO[] examitemvos) throws BusinessException {
    UFDouble retValue = new UFDouble(-1000000000);
    
    if (null == examitemvos || examitemvos.length == 0)
    {
      return retValue;
    }

    
    CreditCheckCore checker = new CreditCheckCore(this.isAddSelf);
    List<CreditCheckMessage> listOfMsg = checker.creditCheck(examitemvos);
    
    if (this.isRateCheck) {
      retValue = getMaxRate(listOfMsg, retValue);
    } else {
      
      retValue = getMaxMny(listOfMsg, retValue);
    } 
    
    return retValue;
  }





  
  private UFDouble getMaxMny(List<CreditCheckMessage> list, UFDouble retValue) {
    UFDouble temp = retValue;
    for (CreditCheckMessage msg : list) {
      if (MathTool.compareTo(msg.getNbigmny(), temp) > 0) {
        temp = msg.getNbigmny();
      }
    } 
    
    return temp;
  }




  
  private UFDouble getMaxRate(List<CreditCheckMessage> listOfMsg, UFDouble retValue) {
    UFDouble temp = retValue;
    UFDouble hundred = new UFDouble(100);
    for (CreditCheckMessage msg : listOfMsg) {
    	if(null == msg.getNmny()) {
      		temp = new UFDouble(10000);
      		}
      if (msg.getNmny().compareTo(UFDouble.ZERO_DBL) == 0) {
        continue;
      }
      UFDouble rate = msg.getNbigmny().div(msg.getNmny()).multiply(hundred);
      if (MathTool.compareTo(rate, temp) > 0) {
        temp = rate;
      }
    } 
    
    return temp;
  }
  
}